<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增商品分类')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-product-category-add">
        <input id="treeId" name="parentId" type="hidden" value="0"/>
        <!--<div class="form-group">-->
            <!--<label class="col-sm-3 control-label "><span style="color: red; ">*</span>上级分类：</label>-->
            <!--<div class="col-sm-8">-->
                <!--<div class="input-group">-->
                    <!--<input class="form-control" type="text" onclick="selectCategoryTree()" id="treeName" readonly="true"-->
                           <!--th:value="${category.categoryName}">-->
                    <!--<span class="input-group-addon"><i class="fa fa-search"></i></span>-->
                <!--</div>-->
            <!--</div>-->
        <!--</div>-->
        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>分类名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="categoryName" id="categoryName" required>
            </div>
        </div>
        <!--<div class="form-group">-->
            <!--<label class="col-sm-3 control-label">分类封面图：</label>-->
            <!--<div class="col-sm-8">-->
                <!--<input class="form-control" type="file" id="imageBtn" name="imageBtn" onchange="uploadImage(1)"/>-->
                <!--<input class="form-control" name="coverUrl" id="imageUrl" type="hidden">-->
                <!--<span class="help-block m-b-none" style="color: red; "><i class="fa fa-info-circle"></i>封面图推荐尺寸为：60px × 60px</span>-->
            <!--</div>-->
        <!--</div>-->
        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>显示排序：</label>
            <div class="col-sm-8">
                <input class="form-control" type="number" name="orderNum" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>分类状态：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('show_status')}">
                    <input type="radio" th:id="${dict.dictCode}" name="isEnable" th:value="${dict.dictValue}"
                           th:checked="${dict.default}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script type="text/javascript">
    var prefix = ctx + "back/productCategory";

    $("#form-product-category-add").validate({
        onkeyup: false,
        rules: {
            categoryName: {
                remote: {
                    url: prefix + "/checkCategoryNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "parentId": function () {
                            return $("input[name='parentId']").val();
                        },
                        "categoryName": function () {
                            return $.common.trim($("#categoryName").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            orderNum: {
                digits: true
            },
        },
        messages: {
            "categoryName": {
                remote: "分类已经存在"
            }
        },
        focusCleanup: true
    });

    // //上传图片文件
    // $("#imageBtn").fileinput({
    //     language: 'zh', //设置语言
    //     dropZoneTitle: '拖拽文件到这里 &hellip;<br>仅支持上传一张图片<br>多次上传替换前一张图片',
    //     allowedFileExtensions: ['jpg', 'png', 'bmp', 'jpeg'],//接收的文件后缀
    //     showUpload: false, //是否上传文件
    //     showRemove: true,
    //     dropZoneEnabled: true,//是否显示拖拽区域
    //     maxFileCount: 1,
    //     msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！"
    // }).on('fileclear', function (event, id) {
    //     $.modal.alertSuccess("删除图片成功！")
    //     $("#imageUrl").val('');
    // });
    //
    // //通用上传文件
    // function ajaxUploadFile(callback, file, modelType) {
    //     var formdata = new FormData();
    //     formdata.append("file", file);
    //     formdata.append("modelType", modelType);
    //     $.modal.loading("上传中...");
    //     $.ajax({
    //         url: ctx + "back/upload/image",
    //         data: formdata,
    //         type: "post",
    //         processData: false,
    //         contentType: false,
    //         success: function (result) {
    //             $.modal.closeLoading();
    //             callback(result);
    //         },
    //         error: function () {
    //             $.modal.closeLoading();
    //             $.modal.alertWarning("图片上传失败");
    //         }
    //     })
    // }
    //
    // //图片文件上传
    // function uploadImage(modelType) {
    //     var files = document.getElementById('imageBtn');
    //     if (files) {
    //         ajaxUploadFile(function (result) {
    //             if (result.code == 0) {
    //                 $.modal.alertSuccess("上传图片成功！")
    //             } else {
    //                 $.modal.alertError("上传图片失败，请重试");
    //             }
    //             $("#imageUrl").val(result.url);
    //         }, files.files[0], modelType);
    //     }
    // }

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-product-category-add').serialize());
        }
    }

    /*分类管理-新增-选择父分类树*/
    function selectCategoryTree() {
        var options = {
            title: '分类选择',
            width: "380",
            url: prefix + "/selectCategoryTree/" + $("#treeId").val(),
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var body = layer.getChildFrame('body', index);
        $("#treeId").val(body.find('#treeId').val());
        $("#treeName").val(body.find('#treeName').val());
        layer.close(index);
    }
</script>
</body>
</html>
